import { useState, useEffect} from 'react';
function useLocalStorage(key) {
    /**
     * bug: Error: A cross-origin error was thrown. React doesn't have access to 
     * the actual error object in development. 
     * See https://reactjs.org/link/crossorigin-error for more information.
     * 老师的代码没有问题 没找出来原因 代码一模一样
     * 
     * bug好像自己好了....
     * 
     */

    const [name, setName] = useState(() => {
        const name = JSON.parse(window.localStorage.getItem(key));
        // const name = window.localStorage.getItem(key);

        return name; 
      });
    
      useEffect(() => {
        window.localStorage.setItem(key, JSON.stringify(name));
      }, [name]);
    
      return [name, setName];
}

export default useLocalStorage;